﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eis51.CampanyCMS.Model.DownLoadMgnt;
using Eis51.CampanyCMS.Business.DownLoadMgnt;
using Eis51.CampanyCMS.FrameWork;

namespace Eis51.Web.Admin.DownLoadFileMgnt
{
    public partial class list_type : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnSearch_Click(null, null);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.dgNews.Data = DownloadFileTypeBLL.GetInstance().Select();
            this.dgNews.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("edit_type.aspx?r=" + Server.UrlDecode(Request.RawUrl));
        }

        public string GetTitle(object obj)
        {
            string title = string.Empty;
            if (obj != null)
            {
                switch (int.Parse(obj.ToString()))
                { 
                    case (int)IndexImgPosition.Product:
                        title = "产品";
                        break;
                    case (int)IndexImgPosition.Customer:
                        title = "客户案例";
                        break;
                    case (int)IndexImgPosition.Purchase:
                        title = "购买信息";
                        break;
                    case (int)IndexImgPosition.News:
                        title = "公司新闻";
                        break;
                    case (int)IndexImgPosition.Market:
                        title = "市场活动";
                        break;

                }
            }

            return title;
        }

        protected void dgNews_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    if (!DownloadFileTypeBLL.GetInstance().Candelete(e.CommandArgument.ToString()))
                    {
                        BaseTools.ShowMessage(this.Page, "此类型已经被使用，请先删除相应的下载文章再删除类型！");
                        return ;
                    }

                    DownloadFileType obj = DownloadFileTypeBLL.GetInstance().Get(new DownloadFileType.Key(int.Parse(e.CommandArgument.ToString())));
                    DownloadFileTypeBLL.GetInstance().Delete(obj);
                    btnSearch_Click(null, null);
                    break;
                case "Modify":
                    string url = "edit_type.aspx?typeid={0}&r={1}";
                    url = string.Format(url, e.Item.Cells[0].Text, Server.UrlDecode(Request.RawUrl));
                    Response.Redirect(url);
                    break;
                default:
                    break;
            }
        }

        protected void dgNews_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header)
            {
                CheckBox cbxAll = e.Item.FindControl("cbxAll") as CheckBox;
                if (cbxAll != null)
                    cbxAll.Attributes.Add("onclick", "javascript:return SelectAll('" + dgNews.ClientID + "');");
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {

        }
    }
}
